package com.atguigu.myjuc.demo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 阻塞队列api
 */
public class BlockingQueueDemo {
    public static void main(String[] args) {

        //创建无界阻塞队列
        //BlockingQueue<Integer> queue = new LinkedBlockingDeque<>();

        //创建有界阻塞队列：当消费的进度较慢，生产进度较快，而且队列放不下的时候，生产会被自动阻塞，等待消费
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(3);

        //生产者
        new Thread(()->{
            try {
                for (int i = 1; i <= 10; i++) {
                    queue.put(i);
                    System.out.println("生产第" + i + "个馒头，" + "目前还剩" + queue.size() + "个馒头");
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "生产者").start();

        //消费者
        new Thread(()->{
            try {
                for (int i = 1; i <= 10; i++) {
                    System.out.println("消费第" + queue.take() + "个馒头" + "目前还剩" + queue.size() + "个馒头");
                    TimeUnit.SECONDS.sleep(3);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "消费者").start();
        /*BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
//        System.out.println(blockingQueue.add("a"));
//        System.out.println(blockingQueue.add("b"));
//        System.out.println(blockingQueue.add("c"));
//
//        System.out.println(blockingQueue.element());

//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());

//        System.out.println(blockingQueue.offer("a"));
//        System.out.println(blockingQueue.offer("b"));
//        System.out.println(blockingQueue.offer("c"));
//        System.out.println(blockingQueue.offer("D"));

//        System.out.println(blockingQueue.peek());

//        System.out.println("取数据"+blockingQueue.poll());
//        System.out.println("取数据"+blockingQueue.poll());
//        System.out.println("取数据"+blockingQueue.poll());
//        System.out.println("取数据"+blockingQueue.poll());

//        blockingQueue.put("a");
//        blockingQueue.put("b");
//        blockingQueue.put("c");
//        blockingQueue.put("d");
//        System.out.println("come in");
//
//        System.out.println(blockingQueue.take());
//        System.out.println(blockingQueue.take());
//        System.out.println(blockingQueue.take());
//        System.out.println(blockingQueue.take());

        blockingQueue.offer("a",3L, TimeUnit.SECONDS);
        blockingQueue.offer("b",3L, TimeUnit.SECONDS);
        blockingQueue.offer("c",3L, TimeUnit.SECONDS);
        blockingQueue.offer("e",3L, TimeUnit.SECONDS);
        System.out.println("come in");

        System.out.println(blockingQueue.poll(3L,TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(3L,TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(3L,TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(3L,TimeUnit.SECONDS));*/
    }
}
